package com.zjpavt.client.page.abstractpage;


import com.zjpavt.client.page.editableunit.EditUnit;
import com.zjpavt.client.page.intefacepage.EditablePage;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 可编辑的页面基类
 * @author cjp
 */
@Slf4j
public abstract class AbstractEditablePage extends AbstractBasePage implements EditablePage {

    protected boolean isInEditMode = false;

    protected List<EditUnit> editUnitList = new ArrayList<>();

    /**
     * 当前可编辑块所在的下标 从0开始
     */
    protected int currBlinkIndex = 0;

    public AbstractEditablePage(int pageNum, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
    }

    /**
     * 为可编辑单元设置初始值
     * 需要在进入编辑模式 和 初始化的时候调用
     */
    @Override
    public abstract void setDefaultVal();

    /**
     * 初始化可编辑单元
     */
    @Override
    public abstract void initEditUnits();

    /**
     * 保存修改的参数
     */
    @Override
    public abstract void saveVal();

    @Override
    public void addEditUnit(EditUnit e) {
        editUnitList.add(e);
    }

    @Override
    public void removeEditUnit(EditUnit e) {
        editUnitList.remove(e);
    }

    /**
     * 清空可编辑单元
     */
    @Override
    public void clearEditUnit() {
        editUnitList.clear();
    }

    @Override
    public EditUnit getEditUnit(int index) {
        if (index >= 0 && index < editUnitList.size()) {
            return editUnitList.get(index);
        }
        return null;
    }

    /**
     * begin with 0
     * @return
     */
    @Override
    public int getCurrBlinkIndex() {
        return currBlinkIndex;
    }

    @Override
    public void onKeyDown() {
        if (isInEditMode) {
            editUnitList.get(currBlinkIndex).onDown();
        } else {
            super.onKeyDown();
        }
    }

    @Override
    public void onKeyUp() {
        if (isInEditMode) {
            editUnitList.get(currBlinkIndex).onUp();
        } else {
            super.onKeyUp();
            currBlinkIndex = 0;
        }
    }

    @Override
    public void onKeySet() {
        super.onKeySet();
        if (isInEditMode) {
            editUnitList.get(currBlinkIndex).stopEdit(true);
            if (currBlinkIndex == editUnitList.size() - 1) {
                currBlinkIndex = 0;
            } else {
                currBlinkIndex++;
            }
            log.debug(" onKeySet currBlinkIndex :" + currBlinkIndex);
            EditUnit editUnitNow = editUnitList.get(currBlinkIndex);
            log.debug("line num :" + editUnitNow.getLineNum()
                      + " start pos:" + editUnitNow.getStartPos()
                      + "end pos " + editUnitNow.getEndPos());
            editUnitNow.startEdit();
        } else {
            enterEditMode();
        }
    }

    @Override
    public void onKeyEnt() {
        super.onKeyEnt();
        if (isInEditMode) {
            exitEditMode();
        }
    }

    /**
     * 进入编辑模式
     */
    @Override
    public void enterEditMode() {
        isInEditMode = true;
        cancelAutoRefreshing();
        setDefaultVal();
        editUnitList.get(currBlinkIndex).startEdit();
    }

    @Override
    public void exitEditMode() {
        isInEditMode = false;
        editUnitList.get(currBlinkIndex).stopEdit(false);
        currBlinkIndex = 0;
//		先刷新再保存 保证页面显示的流畅
        startAutoRefreshing();
        saveVal();
        refreshPage();
    }

    @Override
    public boolean isInEditMode() {
        return isInEditMode;
    }

    @Override
    public int getEditUnitSize() {
        return this.editUnitList.size();
    }

    @Override
    public void init() {
        initEditUnits();
    }

    @Override
    public void startAutoRefreshing() {
        if (!isInEditMode()) {
            super.startAutoRefreshing();
        } else {
            log.debug("is edit don't refresh" + getPageNum());
        }
    }

    @Override
    public void close() {
        currBlinkIndex = 0;
    }
}
